package handlers

import (
	"github.com/rs/xid"
	"github.com/shugen/models"
	"github.com/shugen/utils"
	"net/http"
	"strconv"
	"time"
)

//门神关系
func (c *Controller) IMenshen(w http.ResponseWriter, r *http.Request) {

	if r.Method != "POST" {
		return
	}

	type_value, _ := strconv.Atoi(r.PostFormValue("type"))
	menshen := &models.MenShen{
		MenShenId: []byte(xid.New().String()),
		Type:      type_value,
		Title:     r.PostFormValue("title"),
		Desc:      r.PostFormValue("desc"),
		Status:    1,
		CreatedAt: time.Now().UTC(),
		UpdatedAt: time.Now().UTC(),
	}
	if err := c.Svc.InsertMenShen(menshen); err != nil {
		w.Write(utils.Fail(err))
		return
	}
	w.Write(utils.Success(menshen))

}

//update 门神关系
func (c *Controller) UMenshen(w http.ResponseWriter, r *http.Request) {

	ms := &models.MenShen{
		MenShenId: []byte(r.PostFormValue("MenShenId")),
		//Type:      0,
		Title:  r.PostFormValue("title"),
		Desc:   r.PostFormValue("desc"),
		Status: 0,
		//CreatedAt: time.Time{},
		UpdatedAt: time.Now().UTC(),
	}

	menshen, err := c.Svc.UpdateMenShen(ms)

	if err != nil {
		w.Write(utils.Fail(err))
		return
	}
	w.Write(utils.Success(menshen))
}

//select 门神关系
func (c *Controller) SMenshen(w http.ResponseWriter, r *http.Request) {

	ms, err := c.Svc.SelectMenShen(&models.MenShen{MenShenId: []byte(r.PostFormValue("MenShenId"))})

	if err != nil {
		w.Write(utils.Fail(err))
		return
	}
	w.Write(utils.Success(ms))
}

//select List 门神关系
func (c *Controller) LMenshen(w http.ResponseWriter, r *http.Request) {

	ms, err := c.Svc.ListMenShen()

	if err != nil {
		w.Write(utils.Fail(err))
		return
	}
	w.Write(utils.Success(ms))
}
